﻿using CommunityToolkit.Mvvm.Input;
using DBContext;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;
using ZoneLine.Models.PackageModel;
using ZoneLine.Models.XWStations;

namespace ZoneLine.ViewModels
{
    public class StationResultViewModel : ViewModelBase
    {
        private IModelDAL _dal;
        public StationResultViewModel(IModelDAL dal)
        {
            _dal = dal;
        }

        private bool _enableQrCode = true;

        public bool EnableQrCode
        {
            get { return _enableQrCode; }
            set => SetProperty(ref _enableQrCode, value);
        }

        private bool _enableAbsCode;

        public bool EnableAbsCode
        {
            get { return _enableAbsCode; }
            set => SetProperty(ref _enableAbsCode, value);
        }



        private List<object> _results;

        public List<object> Results
        {
            get { return _results; }
            set => SetProperty(ref _results, value);
        }

        private string _qrCode;

        public string QrCode
        {
            get { return _qrCode; }
            set => SetProperty(ref _qrCode, value);
        }

        private DateTime _dt = DateTime.Now;

        public DateTime Dt
        {
            get { return _dt; }
            set => SetProperty(ref _dt, value);
        }

        private string _targetStation = string.Empty;

        private RelayCommand<string> _setStationCmd;

        public RelayCommand<string> SetStationCmd
        {
            get { return _setStationCmd ?? (_setStationCmd = new RelayCommand<string>(SetStation)); }
        }

        private void SetStation(string obj)
        {
            _targetStation = obj;
        }

        private RelayCommand _queryCmd;

        public RelayCommand QueryCmd
        {
            get { return _queryCmd ?? (_queryCmd = new RelayCommand(QueryData)); }
        }

        private void QueryData()
        {
            var station = _targetStation;
            var code = QrCode;
            var queryByCode = !string.IsNullOrEmpty(QrCode);
            if (queryByCode)
            {
                if (EnableQrCode)
                {
                    var printRlt = _dal.GetModel<AbsPrintResult>(p => p.Code == QrCode);
                    if (printRlt == null)
                    {
                        Results = new List<object>();
                        return;
                    }
                    else
                    {
                        code = printRlt.AbsCode;
                    }
                }
            }
            dynamic rlts = new List<object>();

            switch (station)
            {
                case "QOP006":
                    {
                        if (queryByCode)
                        {

                            rlts = _dal.GetModelList<QOP006StationResult>(p => p.AbsScanCode == code);
                        }
                        else
                        {
                            rlts = _dal.GetModelList<QOP006StationResult>(p => p.CreateTime >= Dt.Date);
                        }
                    }
                    break;
                case "QOP010":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<QOP010StationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<QOP010StationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "QOP020":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<QOP020StationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<QOP020StationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "QOP050L":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<QOP050LStationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<QOP050LStationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "QOP050R":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<QOP050RStationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<QOP050RStationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;

                case "HOP010":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<HOP010StationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<HOP010StationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;

                case "HOP040":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<HOP040StationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<HOP040StationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;


                case "XW010S":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<XW010SingleStationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<XW010SingleStationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "XW010L":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<XW010LStationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<XW010LStationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "XW010R":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<XW010RStationResult>(p => p.AbsScanCode == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<XW010RStationResult>(p => p.CreateTime >= Dt.Date);
                    }
                    break;
                case "QPrintCode":
                    if (queryByCode)
                    {
                        rlts = _dal.GetModelList<AbsPrintResult>(p => p.Code == code);
                    }
                    else
                    {
                        rlts = _dal.GetModelList<AbsPrintResult>(p => p.PrintTime >= Dt.Date);
                    }
                    break;


                default:
                    break;

            }
            var modelsTemp = new List<object>();
            foreach (var item in rlts)
            {
                modelsTemp.Add(item);
            }
            Results = modelsTemp;
        }

        private StationResultBase _rltTarget;

        public StationResultBase RltTarget
        {
            get { return _rltTarget; }
            set { _rltTarget = value; }
        }


        private RelayCommand _delCmd;

        public RelayCommand DelCmd
        {
            get { return _delCmd ?? (_delCmd = new RelayCommand(Del)); }
        }

        private void Del()
        {
            if (_rltTarget != null)
            {
                var rlt = GetConfirmRlt("请确认是否要删除该条数据");
                if (rlt)
                {
                    var rltType = RltTarget.GetType();
                    var name = rltType.Name;

                    var delRlt = DbConfig.DbContext.Ado.ExecuteNonQuery($"delete from {name} where Id='{RltTarget.Id}'");
                    if (delRlt > 0)
                    {
                        QueryData();
                        //删除成功
                    }
                }
            }
        }
    }
}
